var myDatagrid;
var searchForm;
var editIndex = -1;
var comboData = {};

var currentDatagrid; //当前选项卡的动态表格对象
var falg = 1;    //当前所在页  1同义词 2敏感词

// 显示初始化数据
$(function () {
    currentDatagrid = $('#mydatagrid');
    myDatagrid = currentDatagrid.datagrid({
        onAfterEdit: function (rowIndex, rowData, changes) {//在用户完成编辑一行的时候触发，参数包括：rowIndex：编辑行的索引，索引从0开始。rowData：对应于完成编辑的行的记录。changes：更改后的字段(键)/值对。
            //成功完成编辑，包括校验
            editIndex = -1;
        },
        onLoadSuccess: function (data) {
            if (editIndex != -1) {
                myDatagrid.datagrid('cancelEdit', editIndex);
                editIndex = -1;
            }
        }
    });
});

function endEditing() {
    if (editIndex == -1) {
        return true;
    }
    if (currentDatagrid.datagrid('validateRow', editIndex)) {
        currentDatagrid.datagrid('endEdit', editIndex);
        editIndex = -1;
        return true;
    } else {
        return false;
    }
}

function onClickRow(rowIndex, field, value) {
    if (editIndex != rowIndex) {
        if (endEditing()) {
            currentDatagrid.datagrid('selectRow', rowIndex).datagrid('beginEdit', rowIndex);
            editIndex = rowIndex;
        } else {
            currentDatagrid.datagrid('selectRow', editIndex);
            //commonLang.tip.alert替换'提示',commonLang.optip.add.dataIntegrity替换'请保证数据填写完整！'
            $.messager.alert(commonLang.tip.alert, commonLang.optip.add.dataIntegrity, 'warning');
        }
    }
}

//增加一条记录
function addData() {
    if (endEditing()) {
        currentDatagrid.datagrid('appendRow', {
            id: '',
            attribute01:falg
        });
        editIndex = currentDatagrid.datagrid('getRows').length - 1;
        currentDatagrid.datagrid('selectRow', editIndex).datagrid('beginEdit', editIndex);

    } else {
        //commonLang.tip.alert替换'提示',commonLang.optip.add.dataIntegrity替换'请保证数据填写完整！'
        $.messager.alert(commonLang.tip.alert, commonLang.optip.add.dataIntegrity, 'warning');
    }
}



//去除空格
function trim(str) {
    return str.replace(/(^\s+)|(\s+$)/g, "");
}

//保存数据
function saveData() {
    var msg = "关键字";
    if (falg == 2) {
        msg = "敏感字"
    }
    currentDatagrid.datagrid('endEdit', editIndex);
    var rows = currentDatagrid.datagrid('getChanges');
    if (rows == '') {
        currentDatagrid.datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
        currentDatagrid.datagrid('reload', {});
        parent.parent.parent.$.messager.show({
            title: '提示',
            //commonLang.optip.save.noSave替换'没有可保存的数据！'
            msg: commonLang.optip.save.noSave
        });
        return;
    }
    for (var i = 0; i < rows.length; i++) {
        var row = rows[i];
        var formalThemeWord = row.formalThemeWord;
        var unformalThemeWord = row.unformalThemeWord;
        if (formalThemeWord == "" || formalThemeWord == undefined) {
            //commonLang.tip.alert替换'提示',commonLang.optip.save.officialKeywordNotNull替换'正式关键字不能为空！'
            $.messager.alert(commonLang.tip.alert, msg + "不能为空!", 'warning');
            currentDatagrid.datagrid('beginEdit', editIndex);
            return;
        }
        if (falg == 1) {
            if (unformalThemeWord == "" || unformalThemeWord == undefined) {
                //commonLang.tip.alert替换'提示',commonLang.optip.save.informalityKeywordNotNull替换'非正式关键字不能为空！'
                $.messager.alert(commonLang.tip.alert, "同义词不能为空!", 'warning');
                currentDatagrid.datagrid('beginEdit', editIndex);
                return;
            }
        }
        row.formalThemeWord = trim(row.formalThemeWord);
        if (!row.formalThemeWord) {
            //commonLang.tip.alert替换'提示',commonLang.optip.save.officialKeywordNotNull替换'正式关键字不能为空！'
            $.messager.alert(commonLang.tip.alert, msg + "不能为空!", 'warning');
            currentDatagrid.datagrid('beginEdit', editIndex);
            return;
        }
        if (falg == 1) {
            row.unformalThemeWord = trim(row.unformalThemeWord);
            if (!row.unformalThemeWord) {
                //commonLang.tip.alert替换'提示',commonLang.optip.save.informalityKeywordNotNull替换'非正式关键字不能为空！'
                $.messager.alert(commonLang.tip.alert, "同义词不能为空", 'warning');
                currentDatagrid.datagrid('beginEdit', editIndex);
                return;
            }
        }
        var formalThemeWordLen = row.formalThemeWord.replace(/([^\u0000-\u00FF])/g, "aa").length;
        if (formalThemeWordLen >= 200) {
            //commonLang.tip.alert替换'提示',commonLang.optip.save.officialKeywordLength替换'正式关键字长度应该介于1到200之间！'
            $.messager.alert(commonLang.tip.alert, msg + "长度应该介于1到200之间！", 'warning');
            currentDatagrid.datagrid('beginEdit', editIndex);
            return;
        }
        if (falg == 1) {
            var unformalThemeWordLen = row.unformalThemeWord.replace(/([^\u0000-\u00FF])/g, "aa").length;
            if (unformalThemeWordLen >= 200) {
                //commonLang.tip.alert替换'提示',commonLang.optip.save.informalityKeywordLength替换'非正式关键字长度应该介于1到200之间！'
                $.messager.alert(commonLang.tip.alert, "同义词长度应该介于1到200之间！", 'warning');
                currentDatagrid.datagrid('beginEdit', editIndex);
                return;
            }
        }
    }
    var data = JSON.stringify(rows);
    if (rows.length > 0) {
        $.ajax({
            url: 'platform/LantopLexiconController/saveData',
            data: {
                datas: data
            },
            type: 'post',
            dataType: 'json',
            success: function (r) {
                if (r.flag == "success") {
                    currentDatagrid.datagrid('reload', {});
                    currentDatagrid.datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
                    parent.parent.parent.$.messager.show({
                        title: '提示',
                        //commonLang.optip.save.success替换'保存成功!'
                        msg: commonLang.optip.save.success
                    });
                    currentDatagrid.datagrid('beginEdit', editIndex);
                } else {
                    //commonLang.optip.save.fail替换'保存失败!'
                    $.messager.alert('提示', commonLang.optip.save.fail + r.flag, 'warning');
                    currentDatagrid.datagrid('reload', {});
                    currentDatagrid.datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
                }
            }
        });
    }
}


/*删除选中数据*/
function deleteData() {
    var rows = currentDatagrid.datagrid('getSelections');
    var ids = [];
    if (rows.length > 0) {
        for (var i = 0; i < rows.length; i++) {
            if (null != rows[i].id && "" != rows[i].id) {
                ids.push(rows[i].id);
            }
        }
        if (ids.toString() == "") {
            //commonLang.tip.confirm替换'请确认',commonLang.optip.del.removeRecord替换'确定移除记录？'
            $.messager.confirm(commonLang.tip.confirm, commonLang.optip.del.removeRecord, function (b) {
                if (b) {
                    for (var i = 0; i < rows.length; i++) {
                        var index = currentDatagrid.datagrid('getRowIndex', rows[i]);
                        currentDatagrid.datagrid('cancelEdit', index).datagrid('deleteRow', index);
                    }
                }
            });
        } else {
            if (ids.toString().charAt(ids.toString().length - 1) != ',') {
                //commonLang.tip.confirm替换'请确认',commonLang.optip.del.deleteData替换'您确定要删除当前所选的数据？'
                $.messager.confirm(commonLang.tip.confirm, commonLang.optip.del.deleteData, function (b) {
                    if (b) {
                        for (var i = 0, length = rows.length; i < length; i++) {
                            ids.push(rows[i].ID);
                        }
                        if (ids.toString() != "") {
                            $.ajax({
                                url: 'platform/LantopLexiconController/deleteData',
                                data: {
                                    ids: ids.join(',')
                                },
                                type: 'post',
                                dataType: 'json',
                                success: function (r) {
                                    currentDatagrid.datagrid('reload', {});
                                    currentDatagrid.datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
                                    editIndex = -1;
                                    parent.parent.parent.$.messager.show({
                                        title: '提示',
                                        //commonLang.optip.del.success替换'删除成功！'
                                        msg: commonLang.optip.del.success
                                    });
                                }
                            });
                        }
                    }
                });
            } else {
                //commonLang.tip.confirm替换'请确认',commonLang.optip.del.deleteData替换'您确定要删除当前所选的数据？'
                $.messager.confirm(commonLang.tip.confirm, commonLang.optip.del.deleteData, function (b) {
                    if (b) {
                    	parent.parent.parent.$.messager.show({
                            title: '提示',
                            //commonLang.optip.del.fail替换'删除失败！'
                            msg: commonLang.optip.del.fail
                        });
                    }
                });

            }
        }

    } else {
        //commonLang.tip.alert替换'提示',commonLang.optip.del.deleteRecord替换'请选择要删除的记录！'
        $.messager.alert(commonLang.tip.alert, commonLang.optip.del.deleteRecord, 'warning');
    }
}

function onSelect(title, index) {
    editIndex = -1;
    if (title == "同义词") {
        falg = 1;
        currentDatagrid = $('#mydatagrid');
        currentDatagrid.datagrid({
            isField: 'id',
            rownumbers: false,
            animate: true,
            collapsible: false,
            fitColumns: true,
            autoRowHeight: false,
            scrollbarSize:0,
            singleSelect: false,
            pagination: true,
            pageSize: 10,
            pageList: [10, 15, 20],
            fit: true,
            method: 'post',
            url: 'platform/LantopLexiconController/getThemeWordInfo?falg=' + falg,
            toolbar: '#toolbar',
            onClickRow: onClickRow
        })

    }
    if (title == "敏感词") {
        falg = 2;
        currentDatagrid = $('#mydatagrid1');
        currentDatagrid.datagrid({
            isField: 'id',
            rownumbers: false,
            animate: true,
            collapsible: false,
            fitColumns: true,
            scrollbarSize:0,
            autoRowHeight: false,
            singleSelect: false,
            pagination: true,
            pageSize: 10,
            pageList: [10, 15, 20],
            fit: true,
            method: 'post',
            url: 'platform/LantopLexiconController/getThemeWordInfo?falg=' + falg,
            toolbar: '#toolbar',
            onClickRow: onClickRow
        })
    }
}

function fastSearch(){
    if (event.keyCode == 13) {
        currentDatagrid.datagrid("load",{
            param:$("#evalSearch").val(),
            falg:falg
        })
    }
}

//添加序号
function getPageSize(value, row, index){
    index+=1;//设置起始序号为1，如果不加这行代码序号就会从0开始
    return index++;//根据行的数量不断自增
}
